﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 调查分页列表。
	/// </summary>
	public class VotePagingList : VoteList
	{
		#region
		/// <summary>
		/// 地址栏查询当前页字段。
		/// </summary>
		public virtual string PageField
		{
			get { return GenericUrlManager.Vote_Query_PageField; }
		}

		/// <summary>
		/// 列表Url模式。
		/// </summary>
		public virtual string ListUrlPattern
		{
			get { return GenericUrlManager.BuildVoteListUrlPattern(ClassCode); }
		}
		#endregion

		/// <summary>
		/// 分页控件。
		/// </summary>
		protected Wuqi.Webdiyer.AspNetPager CtrPager;

		private LoadedValue<string> loadedClassCode;
		public override string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => Request.QueryString[GenericUrlManager.Vote_Query_ClassCodeField];
				return loadedClassCode.Value;
			}
		}

		private LoadedValue<int?> loadedChannelId;
		public override int? ChannelId
		{
			get
			{
				if (loadedChannelId.Loader == null)
					loadedChannelId.Loader = () => IntExtensions.ParseOrNull(Request.QueryString[GenericUrlManager.Vote_Query_ChannelField]);
				return loadedChannelId.Value;
			}
		}

		private LoadedValue<string> loadedKeyword;
		/// <summary>
		/// 求职信息列表关键字。
		/// </summary>
		public override string Keyword
		{
			get
			{
				if (loadedKeyword.Loader == null)
					loadedKeyword.Loader = () => Request.QueryString["Keyword"];
				return loadedKeyword.Value;
			}
		}

		private LoadedValue<int> loadedCurrentPage;
		/// <summary>
		/// 求职信息当前页码。
		/// </summary>
		public virtual int CurrentPage
		{
			get
			{
				if (loadedCurrentPage.Loader == null)
					loadedCurrentPage.Loader = () => IntExtensions.ParseOrDefault(Request.QueryString[PageField], 1);
				return loadedCurrentPage.Value;
			}
		}

		private LoadedValue<SearchType> loadedSearchType;
		/// <summary>
		/// 求购信息列表检索类别。
		/// </summary>
		public virtual SearchType SearchType
		{
			get
			{
				if (loadedSearchType.Loader == null)
					loadedSearchType.Loader = () => EnumExtensions.ParseOrDefault<SearchType>(Request.QueryString["SearchType"], SearchType.None);
				return loadedSearchType.Value;
			}
		}




		/// <summary>
		/// 列表初始化。
		/// </summary>
		/// <param name="e">事件参数。</param>
		/// <remarks>初始化分页控件的 UrlPageIndexName 属性。根据搜索类型确定是否使用Url重写模式。</remarks>
		protected override void OnInit(EventArgs e)
		{
			CtrPager.UrlPageIndexName = PageField;
			if (SearchType != SearchType.None)
			{
				CtrPager.EnableUrlRewriting = false;
			}
			else
			{
				CtrPager.EnableUrlRewriting = true;
				CtrPager.UrlRewritePattern = ListUrlPattern;
			}
			base.OnInit(e);
		}

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>列表数据。</returns>
		public override object GetListData()
		{
			PaginationInfo<List<Votes.VoteForBaseList>> pageInfo = Votes.GetPaginationBaseList(ClassId, Keyword, CtrPager.PageSize, CurrentPage);
			CtrPager.RecordCount = pageInfo.RecordCount;
			CtrPager.CustomInfoHTML = GenericHelper.GetPagerCustomInfoHtml<List<Votes.VoteForBaseList>>(pageInfo);
			return pageInfo.Records;
		}

		protected virtual void RewriteMeta()
		{
			this.Page.Title = (VoteClassInfo == null ? Res.VoteList : VoteClassInfo.ClassName) +
				(CurrentPage > 1 ? Hxsoft.AppV1.Page.TitleSectionSeparator + string.Format(Res.TitlePaging, CurrentPage) : "")
				+ Hxsoft.AppV1.Page.TitleSuffix;
		}

		protected override void OnPreRender(EventArgs e)
		{
			RewriteMeta();

			base.OnPreRender(e);
		}
	}
}
